#include "impressionfacture.h"

ImpressionFacture::ImpressionFacture(QWidget *parent)
{
    this->setParent(parent);
}
ImpressionFacture::~ImpressionFacture()
{

}
void ImpressionFacture::LancerImpression()
{
    if (m_listeEleve.count() > 0)
    {
        QPrinter *printer = new QPrinter(QPrinter::HighResolution);
        printer->setPaperSize (QPrinter::A4);
        printer->setOrientation(QPrinter::Portrait);
        printer->setFullPage(true);
        QTextDocument *journal_document = new QTextDocument;
        journal_document->setDefaultFont(QFont("Arial", 6));
        journal_document->setHtml(m_listeEleve.ListeElevePourImpression(m_mois, 2011));
        printer->setOutputFormat(QPrinter::NativeFormat);

//        QTextEdit *texte = new QTextEdit;
//        //texte->setDefaultFont(QFont("Arial", 8));
//        texte->setHtml(maListeAImprimer.ListeElevePourImpression(factureDuMoisDeDateEdit->date().month(), 2011));

//        QTextCursor cursor(texte->textCursor());
//        cursor.movePosition(QTextCursor::Start);

        //QTextTable *table = cursor.insertTable(3, 3);
        //QPrinter Printer(QPrinter::HighResolution);
        //Printer.setPageSize(QPrinter::A4);
        QPrintDialog *dialog = new QPrintDialog(printer, this);
        if (dialog->exec() == QDialog::Accepted)
        {
            journal_document->print(printer);

            //texte->document()->print(&Printer);
            this->close();
        }
    }
}
void ImpressionFacture::ExportPDF()
{
    if (m_listeEleve.count() > 0)
    {
        QTextEdit *texte = new QTextEdit;
        texte->setHtml(m_listeEleve.ListeElevePourImpression(m_mois, m_listeEleve.GetAnnee()));

        QTextCursor cursor(texte->textCursor());
        cursor.movePosition(QTextCursor::Start);
        QString fileName = "FactureUnisson-" + QDate::longMonthName(m_mois) + "-" + QString::number(m_listeEleve.GetAnnee()) + ".pdf";
        fileName = QFileDialog::getSaveFileName(this, "Export PDF",
                                                        fileName, "*.pdf");
         if (!fileName.isEmpty()) {
             if (QFileInfo(fileName).suffix().isEmpty())
                 fileName.append(".pdf");
             QPrinter printer(QPrinter::HighResolution);
             printer.setOutputFormat(QPrinter::PdfFormat);
             printer.setOutputFileName(fileName);
             texte->document()->print(&printer);
             this->close();
         }
    }
}
void ImpressionFacture::ExportRTF()
{
    if (m_listeEleve.count() > 0)
    {
        QTextEdit *texte = new QTextEdit;
        texte->setText(m_listeEleve.ListeElevePourSauvegardeRTF(m_mois,m_listeEleve.GetAnnee()));

        QTextCursor cursor(texte->textCursor());
        cursor.movePosition(QTextCursor::Start);
        QString fileName = "FactureUnisson-" + QDate::longMonthName(m_mois) + "-" + QString::number(m_listeEleve.GetAnnee()) +  ".rtf";
        fileName = QFileDialog::getSaveFileName(this, "Export TXT",
                                                             fileName, "*.rtf");
         if (!fileName.isEmpty()) {
             if (QFileInfo(fileName).suffix().isEmpty())
                 fileName.append(".html");
             QFile data(fileName);
             if (!data.open(QIODevice::WriteOnly)) {
                 QMessageBox::information(NULL , OUVERTUREFICHIERIMPOSSIBLE, data.errorString());
                 return;
             }

            QTextStream out(&data);

            out << m_listeEleve.ListeElevePourSauvegardeRTF(m_mois,m_listeEleve.GetAnnee());
            this->close();
//            const QString nom = "file:///" + fileName;
//            QDesktopServices::openUrl(QUrl(nom, QUrl::TolerantMode));
         }
    }

}
